local jass = require 'jass.common'
local handle_manager = handle_manager_class:create()

local weather = {}
setmetatable(weather, weather)
ac.weather = weather

weather.allweathers = { }

--- 天气默认表
weather.default = {
    ["大雨"] = FourCC 'RAhr',
    ["小雨"] = FourCC 'RAlr',
    ["大雾"] = FourCC 'FDwh',
    ["小雾"] = FourCC 'FDwl',
    ["大雪"] = FourCC 'SNhs',
    ["小雪"] = FourCC 'SNls',
    ["小风"] = FourCC 'WOcw',
    ["大风"] = FourCC 'WOlw',
    ["日光"] = FourCC 'LRaa',
    ["月光"] = FourCC 'LRma',
}

-- 创建天气效果
function weather.create(name, rct, is_enable)
    if not weather.default[name] then 
        log.error("创建不存在的天气类型", name)
        return 
    end 
    local handle = jass.AddWeatherEffect(ac.rect.j_temp(rct), weather.default[name] )
    if handle == 0 then
        -- log.error("创建天气效果失败", name)
        -- return 
    end 
    local wea = setmetatable({}, weather)
    local index = handle_manager:allocate()
    wea._index = index
    wea.handle = handle
    weather.allweathers[index] = wea
    if is_enable then 
        wea:enable()
    end
    return wea
end

local mt = {}
weather.__index = mt 

-- 结构
mt.type = 'weather'

-- 句柄(伪)
mt.handle = jass.ConvertUnitState(0)

-- 闪电id
mt.id = '白杨谷之雨(大雨)'

-- 启用天气效果
function mt:enable()
    if not self.removed then 
        jass.EnableWeatherEffect(self.handle, true)
    end 
end 

-- 禁用天气效果
function mt:disable()
    if not self.removed then 
        jass.EnableWeatherEffect(self.handle, false)
    end 
end 

-- 删除天气效果
function mt:remove()
    if not self.removed then 
        self.removed = true 
        local index = self._index 
        handle_manager:free(index)
        weather.allweathers[index] = nil
        jass.RemoveWeatherEffect(self.handle)
    end 
end 

return weather 